package cn.calm.xhtml.impl.ui;

import java.util.Collection;

import cn.calm.xhtml.annotation.Attribute;
import cn.calm.xhtml.annotation.Tag;
import cn.calm.xhtml.api.NodeProcess;
import cn.calm.xhtml.api.exception.XhtmlException;
import cn.calm.xhtml.api.service.ResponseWriter;
import cn.calm.xhtml.api.service.XhtmlContext;
@Tag(namespace = "http://www.calm.cn/xhtml/extras/ui", attributes = {
		@Attribute(name = "items", description = "列表项目", template = "<label></label><input name=\"items\"/>"),
		@Attribute(name = "size", description = "展示个数", template = "<label></label><input name=\"size\"/>"),
		@Attribute(name = "blankValue", description = "默认选项值", template = "<label></label><input name=\"blankValue\"/>"),
		@Attribute(name = "blankText", description = "默认选项", template = "<label></label><input name=\"blankText\"/>"),
		@Attribute(name = "valueProperty", description = "提交值属性", template = "<label></label><input name=\"valueProperty\"/>"),
		@Attribute(name = "textProperty", description = "展示值属性", template = "<label></label><input name=\"textProperty\"/>"),
		@Attribute(name = "childrenProperty", description = "子项目属性", template = "<label></label><input name=\"childrenProperty\"/>")})
public class Select extends InputNode {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final String KEYS_ITEMS = "items";

	public static final String KEYS_SIZE = "size";

	public static final String KEYS_BLANK_VALUE = "blankValue";

	public static final String KEYS_BLANK_TEXT = "blankText";

	public static final String KEYS_VALUE_PROPERTY = "valueProperty";

	public static final String KEYS_TEXT_PROPERTY = "textProperty";

	public static final String KEYS_CHILDREN_PROPERTY = "childrenProperty";

	@Override
	public void processNode(XhtmlContext context) throws XhtmlException {
		String property = getAttr(KEYS_VALUE_PROPERTY);
		if (property == null) {
			throw new XhtmlException();
		}
		String text = getAttr(KEYS_TEXT_PROPERTY);
		if (text == null) {
			throw new XhtmlException();
		}

		createTag(context);
		writeAttr(context, "size", KEYS_SIZE);

		ResponseWriter responseWriter = context.getResponseWriter();

		responseWriter.write(">");

		Object blankValue = getAttrValue(KEYS_BLANK_VALUE, context);

		Object blankText = getAttrValue(KEYS_BLANK_TEXT, context);
		Option op;
		if (blankValue != null || blankText != null) {
			context.setValue("blankValue", blankValue);
			context.setValue("blankText", blankText);

			op = new Option();
			op.setAttr("value", "${blankValue}");
			TextNode tn = new TextNode();
			tn.setContent("${blankText}");
			NodeProcess[] children = new NodeProcess[1];
			children[0] = tn;
			op.setChildren(children);
			op.processNode(context);
		}

		Object items = getAttrValue(KEYS_ITEMS, context);

		if (items != null) {
			Collection<?> ite = convertCollection(items);
			Object value = getAttrValue(KEYS_VALUE, context);
			Object childrenProperty = getAttrValue(KEYS_CHILDREN_PROPERTY, context);

			context.setValue("tempValue", value);

			if (childrenProperty == null) {
				for (Object o : ite) {
					processOption(context, property, text, o);
				}
			} else {
				for (Object o : ite) {
					processOptionGroup(context, property, text, items,
							childrenProperty, o);
				}
			}
		}
		responseWriter.endTag(getTag());
	}

	protected static void processOptionGroup(XhtmlContext context, String property,
			String text, Object items, Object childrenProperty, Object o) {
		context.setValue("r", o);
		Object childrenValue = context.getValue("${r." + childrenProperty + "}");
		if (childrenValue == null) {
			processOption(context, property, text, o);
		}else{
			Collection<?> iteTemp = convertCollection(items);
			if(iteTemp.isEmpty()){
				processOption(context, property, text, o);
			}else{
				OptionGroup og=new OptionGroup();
				og.setAttr(OptionGroup.KEYS_ITEMS, "${r."+childrenProperty+"}");
				og.setAttr("KEYS_CHILDREN_PROPERTY", childrenProperty.toString());
				og.setAttr("label", "${r."+text+"}");
				og.setAttr("value", "${r."+property+"}");
				og.setAttr(KEYS_TEXT_PROPERTY, text);
				og.setAttr(KEYS_VALUE_PROPERTY, property);
				og.processNode(context);
			}
		}
	}

	protected static void processOption(XhtmlContext context, String property,
			String text, Object o) {
		Option op;
		context.setValue("r", o);

		op = new Option();
		op.setAttr("value", "${r." + property + "}");
		op.setAttr(Option.KEYS_SELECTED, "${r." + property
				+ " == tempValue}");
		TextNode tn = new TextNode();
		tn.setContent("${r." + text + "}");
		NodeProcess[] children = new NodeProcess[1];
		children[0] = tn;
		op.setChildren(children);
		op.processNode(context);
	}

	public String getTag() {
		return "select";
	}
}
